package javango.contrib.admin.api;

import javango.core.User;
import javango.db.Manager;
import javango.forms.Form;

public class BaseModelAdmin implements ModelAdmin {

	private String verboseName;

	private String[] fields;
	private String[] listDisplay;
	private String[] listfilter;
	private String[] listSearchFields;
	private String listDisplayLinks;
	
	private String changeFormTemplate = "/javango/contrib/admin/templates/change_form.ftl";
	private String changeListTemplate = "/javango/contrib/admin/templates/change_list.ftl";
	private String searchFormTemplate = "/javango/contrib/admin/templates/search_form.ftl";
	private String confirmDeleteTemplate = "/javango/contrib/admin/templates/confirm_delete.ftl";
	private String indexTemplate = "";
	
	private String editorRole;
	private String authorRole;
	private String readerRole;
	
	AdminAction[] listTools;
	
	String[] orderBy;
	
	private Form form;
	private Form searchForm;
	
	private Manager<?> manager;
	
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getVerboseName()
	 */
	public String getVerboseName() {
		return verboseName;
	}
	public ModelAdmin setVerboseName(String verboseName) {
		this.verboseName = verboseName;
		return this;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getListfilter()
	 */
	public String[] getListfilter() {
		return listfilter;
	}
	public void setListfilter(String[] listfilter) {
		this.listfilter = listfilter;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getFields()
	 */
	public String[] getFields() {
		return fields;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getListDisplay()
	 */
	public String[] getListDisplay() {
		return listDisplay;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getListSearchFields()
	 */
	public String[] getListSearchFields() {
		return listSearchFields;
	}
	
	public ModelAdmin setFields(String... fields) {
		this.fields = fields;
		return this;
	}
	public ModelAdmin setListDisplay(String... fields) {
		this.listDisplay = fields;
		return this;
	}
	public ModelAdmin setListFilter(String... fields) {
		this.listfilter = fields;
		return this;
	}
	public ModelAdmin setListSearchFields(String... fields) {
		this.listSearchFields = fields;
		return this;
	}
	
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getManager()
	 */
	public Manager<?> getManager() {
		return manager;
	}
	public ModelAdmin setManager(Manager<?> manager) {
		this.manager = manager;
		return this;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getListDisplayLinks()
	 */
	public String getListDisplayLinks() {
		return listDisplayLinks;
	}
	public ModelAdmin setListDisplayLinks(String linkField) {
		this.listDisplayLinks = linkField;
		return this;
	}
	/* (non-Javadoc)
	 * @see javango.contrib.admin.api.ModelAdmin#getChangeFormTemplate()
	 */
	public String getChangeFormTemplate() {
		return changeFormTemplate;
	}
	public ModelAdmin setChangeFormTemplate(String changeFormTemplate) {
		this.changeFormTemplate = changeFormTemplate;
		return this;
	}

	public String getEditorRole() {
		return editorRole;
	}
	public ModelAdmin setEditorRole(String editorRole) {
		this.editorRole = editorRole;
		return this;
	}
	public String getAuthorRole() {
		return authorRole;
	}
	public ModelAdmin setAuthorRole(String authorRole) {
		this.authorRole = authorRole;
		return this;
	}
	public String getReaderRole() {
		return readerRole;
	}
	public ModelAdmin setReaderRole(String readerRole) {
		this.readerRole = readerRole;
		return this;
	}
	
	public boolean isAuthorized(User user) {
		return isAuthor(user) || isReader(user) || isEditor(user);
	}
	public boolean isAuthor(User user) {
		return getAuthorRole() == null || user.hasRole(getAuthorRole());
	}
	public boolean isReader(User user) {
		return getReaderRole() == null || user.hasRole(getReaderRole()) || isEditor(user);
	}
	public boolean isEditor(User user) {
		return getEditorRole() == null || user.hasRole(getEditorRole()) || isAuthor(user);
	}
	public ModelAdmin setListTools(AdminAction... actions) {
		this.listTools = actions;
		return this;
	}
	public AdminAction[] getListTools() {
		return listTools;
	}
	public Form getForm() {
		return form;
	}
	public ModelAdmin setForm(Form form) {
		this.form = form;
		return this;
	}
	public Form getSearchForm() {
		return searchForm;
	}
	public ModelAdmin setSearchForm(Form searchForm) {
		this.searchForm = searchForm;
		return this;
	}
	public String getChangeListTemplate() {
		return changeListTemplate;
	}
	public ModelAdmin setChangeListTemplate(String changeListTemplate) {
		this.changeListTemplate = changeListTemplate;
		return this;
	}
	public String getSearchFormTemplate() {
		return searchFormTemplate;
	}
	public ModelAdmin setSearchFormTemplate(String searchFormTemplate) {
		this.searchFormTemplate = searchFormTemplate;
		return this;
	}
	public String getConfirmDeleteTemplate() {
		return confirmDeleteTemplate;
	}
	public ModelAdmin setConfirmDeleteTemplate(String confirmDeleteTemplate) {
		this.confirmDeleteTemplate = confirmDeleteTemplate;
		return this;
	}
	public String getIndexTemplate() {
		return indexTemplate;
	}
	public ModelAdmin setIndexTemplate(String indexTemplate) {		
		this.indexTemplate = indexTemplate;
		return this;
	}
	public String[] getOrderBy() {
		return orderBy;
	}
	public ModelAdmin setOrderBy(String[] orderBy) {
		this.orderBy = orderBy;
		return this;
	}
}
